<?php
if (@$_POST['open'] != 'please') {
    exit;
}
include_once('../../php/include_all.php');
?>
<div class="widget-header widget-header-flat ">
    <h5><i class="icon-calendar"></i>Prioritas Pembangunan</h5>
</div>
<div class="position-relative" id="page-content">
    <a href="#modalwin" data-toggle="modal" class="btn btn-small btn-primary2 no-radius btn-tambah"><i class="icon-plus"></i>&nbsp;Tambah Data</a>
    &nbsp;<span id="load" class="spinner"></span>
</div>

<div id="modalwin" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

    <header class="modal-header">
        <a href="#" class="close btn-close" data-dismiss="modal">x</a>
        <h4>Data Prioritas Pembangunan</h4>
    </header>

    <div class="modal-body">
        <form id="formPRIORITAS" class="form-horizontal" >
            <input type="hidden" name="act" value="simpan_prioritas">
            <input type="hidden" id="id_prioritas" name="id_prioritas" value="0">
            <table class="table-form">
                <tbody>
                    <tr>
                        <td>Kode Prioritas</td>
                        <td>:</td>
                        <td>	
							<?php
							/*	$kode = get_data("select count (*) as mac from mus_prioritas");
								$koded = $kode['mac']+1;
								$kodede = ($koded <=9)? "0".$koded:$koded;
								$kodepri = "Prioritas.".$kodede;
 							*/	
							?>
                            <input type="text" name="kode_prioritas" id="kode_prioritas"> </input>
							
                        </td>
                    </tr>

                    <tr>
                        <td>Nama Prioritas</td>
                        <td>:</td>
                        <td>
                            <textarea type="text" name="nama_prioritas" id="nama_prioritas"></textarea>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td></td>
                        <td>
                            <a class="btn btn-primary2 btn-small bt-simpan"><div id="loader"></div><i class="icon-save"></i>&nbsp;Simpan</a><span id="result"></span>
                        </td>
                    </tr>
                </tbody>
            </table>
        </form>
    </div>
</div>
<div class="box-content">
    <table class="table table-bordered geo-table table-hover" width="100%">
        <thead> 
            <tr>
                <th class="center" width="2%">No</th>
                <th class="center" width="20%">Kode</th>
                <th class="center" width="58%">Prioritas Pembangunan</th>
                <th class="center" width="25%">Aksi</th>
            </tr>
        </thead>
        <tbody id="tampilPRIORITAS">
            <?php
            $x = 1;
            $pr = get_datas("select * from mus_prioritas");
            foreach ($pr as $pr) {
                ?><tr>
                    <td><?php echo $x ?></td>
                    <td><?php echo $pr['kode_prioritas'] ?></td>
                    <td><?php echo $pr['nama_prioritas'] ?></td>
                    <td class="center" >
                        <a href="#modalwin" data-toggle="modal"  class="btn btn-info bt-edit btn-small" name="<?php echo $pr['id_prioritas']; ?>"><i class="icon-edit icon-white"></i> ubah</a>
                        <a class="btn btn-danger bt-hapus btn-small" name="<?php echo $pr['id_prioritas']; ?>"><i class="icon-trash icon-white"></i> hapus</a>

                    </td>
                </tr>
                <?php
                $x++;
            }
            ?>
        </tbody>
    </table>    
</div>
</div>

<script type="text/javascript">
    var url = 'admin/master_prioritas/prioritas_save.php';
    var rslt = $('#result');  
    $('.bt-simpan').click(function(){
        var btn = $(this);
        var load = $('#loader');
        
        load.addClass('icon-spinner icon-spin icon-2x white');
        
        
        var form = $('#formPRIORITAS');
        var data = form.serializeArray();
        var post = $.post(url, data);
        post.done(function(res){
            var result = res.split('__');
            
            if(result.length==3){
                if(result[0]=='success'){
                    rslt.html('<font color="green">Data Tersimpan...</font>');
                    btn.removeClass('btn-info').addClass('btn-primary2');
                    setTimeout(function(){
                        rslt.html('');
                    },1500);
                
                    var tbody = $('#tampilPRIORITAS');
                    tbody.html(result[2]);
                    init();
					$('#id_prioritas').val("0");
                    $("#kode_prioritas").val("");
                    $("#nama_prioritas").val("");
                    $('#modalwin').modal('hide');
                }else{
                    rslt.html('<font color="red">'+res+'</font>');
                }
            }else{
                rslt.html('<font color="red">'+res+'</font>')
            }
            load.removeClass();
            btn.removeClass('.btn-info').addClass('.btn-primary2');
        //    $('#modalwin').html(isiForm);
        });
    });

    $('.btn-tambah').click(function(){ 
		$("#id_prioritas").val("0");
		var post = $.post(url, {act:'get_kode', kode:this.value});
		post.done(function(data){
		//	$('#kode_prioritas').html(data);
		 	document.getElementById('kode_prioritas').value=data;
		});
		
     //   $('#kode_prioritas').val('');
        $('#nama_prioritas').val('');              
    });

    function init(){               
        $('.bt-hapus').click(function(){
            var btn = $(this);
            var pid = this.name;
            var load = $(this);
            load.html('<div class="icon-spinner icon-spin icon-2x white"></div>');
            bootbox.confirm("sumpe lu mau ngehapus item ini?", function(hsl){
                if(hsl==true){
                    if(pid!=0){
                        var post = $.post(url,{act:"hapus_prioritas",id_prioritas:pid});
                        post.done(function(res){
                            var hasil = res.split('__');
                            //alert(hasil.length);
                            if(hasil.length == 2){
                                if(hasil[0] == 'success'){
                                    rslt.html('<font color="green">Data Telah Dihapus...</font>');
                                    setTimeout(function(){
                                        rslt.html('');
                                    }, 1500);
                                    var tbody = $('#tampilPRIORITAS');
                                    tbody.html(hasil[1]);                                   
                                    init();
                                }
                            }
                        });
                        load.html('<i class="icon-trash icon-white"></i> hapus');
                    }
                }else{
                    load.html('<i class="icon-trash icon-white"></i> hapus');
                }
            });
        });
	
        $('.bt-edit').click(function(){
            var id_prioritas= this.name;
            var form = $('#formPRIORITAS');
            var isiForm = $('#modalwin').html();
            $('#temp').html(isiForm);
            var post = $.post(url,{act:'ubah_prioritas',id_prioritas:id_prioritas});
            post.done(function(res){
                var value = res.split('__');  
                //alert(res);
                $('#id_prioritas').val(value[0]);
                form.find('input[name="kode_prioritas"]').val(value[1]);
                form.find('textarea[name="nama_prioritas"]').val(value[2]);		
            });            
            init();
        });
    }

    init();
</script>